/*
Copyright 2011 The Apache Software Foundation

Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package idv.takeshi.model
{
	import idv.takeshi.ApplicationFacade;
	
	import mx.rpc.AsyncToken;
	import mx.rpc.IResponder;
	import mx.rpc.remoting.RemoteObject;
	
	import org.puremvc.as3.interfaces.IProxy;
	import org.puremvc.as3.patterns.proxy.Proxy;
	
	// a proxy for Role remoting access service. 
	public class RoleProxy extends Proxy implements IProxy, IResponder
	{
		public static const NAME:String 							= "RoleProxy";
		
		private static const GET_ALL_ROLE_OPERATION_NAME:String 	= "getAll";
		
		private var roleService:RemoteObject = null;
		
		public function RoleProxy(data:Object=null, endpoint:String = null)
		{
			super(NAME, data);
			
			roleService = new RemoteObject("roleService");
			roleService.endpoint = endpoint;
		}
		
		public function getAllRoles():void
		{
			var token:AsyncToken = roleService.getOperation(GET_ALL_ROLE_OPERATION_NAME).send();
			token.addResponder(this);
		}
		
		
		
		public function result(rpcEvent:Object):void
		{
			var operation:String = rpcEvent.token.message.operation;
			
			switch(operation)
			{
				case GET_ALL_ROLE_OPERATION_NAME:
					data = rpcEvent.result;
					sendNotification(ApplicationFacade.LOAD_ROLES_SUCCESS);
					break;
				default:
					data = null;
			}
		}
		
		public function fault(rpcEvent:Object):void
		{
			var operation:String = rpcEvent.token.message.operation;
			
			switch(operation)
			{
				case GET_ALL_ROLE_OPERATION_NAME:
					data = "Get all roles data failed!";
					sendNotification(ApplicationFacade.LOAD_ROLES_FAILED);
					break;
				default:
					data = null;
			}
		}
	}
}